package cn.openea.perpartner.meily.web.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.openea.perpartner.common.core.utils.ServletUtils;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author Weird077
 * @description TODO
 * @date 2022-12-12 21:29
 */
@Api(tags = "BPM调度")
@RestController
@RequestMapping("/minrest")
public class MinRestController {
    @Value("${bpm.rest.url}")
    private String bpmUrl;
    @Value("${meily.header.sysid}")
    private String sysId;
    @Value("${meily.header.syspwd}")
    private String sysPwd;

    @ApiOperation(value = "统一调度")
    @PostMapping(consumes = "application/json")
    public String transit() throws Exception {
        byte[] bodyBytes = StreamUtils.copyToByteArray(ServletUtils.getRequest().getInputStream());
        String body = new String(bodyBytes, ServletUtils.getRequest().getCharacterEncoding());
        Map<String, Object> formMap = JSON.parseObject(body, Map.class);
        HttpResponse resp = HttpRequest.post(bpmUrl)
                .header("sysid", sysId)
                .header("syspwd", sysPwd)
                .header("userId", "admin")
                .form(formMap)
                .execute();
        return resp.body();
    }
}
